package com.yuandian.business.monitor.controller;

import com.yuandian.api.business_monitor.po.UsersOperationsParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.business.monitor.service.IUserOperationsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


/**
 * <p>
 *  用户操作回溯分析
 * </p>
 *
 * @author zuokun
 * @since 2021-12-6
 */
@RestController
@Api(value = "userOperations", tags = "用户操作回溯分析接口")
@RequestMapping("/alarm/user")
public class UserOperationsController {
    @Autowired
    private IUserOperationsService userOperationsService;

    /**
     * 用户操作概况
     * @param params
     * @return 通用返回对象
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodes",  		value = "业务编码",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "dataCenterList", 		value = "数据中心",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "time", 			value = "时间",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "isAsc", 			value = "排序规则（DESC降序、ASC升序）", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
    @PostMapping("/getUsersOperationsOverview")
	@ApiOperation(value = "用户操作概况", notes = "用户操作概况")
    public R getUsersOperationsOverview(@RequestBody UsersOperationsParams params) {
        return R.ok(userOperationsService.queryUsersOperationsOverview(params));
    }

    /**
     * 查询XX用户操作统计
     * @param params
     * @return 通用返回对象
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodes",  		value = "业务编码",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "dataCenterList", 		value = "数据中心",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "time", 			value = "时间",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "userIdentify", 			value = "用户标识）", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
    @PostMapping("/getUserOperationsStatistics")
	@ApiOperation(value = "查询XX用户操作统计", notes = "查询XX用户操作统计")
    public R getUserOperationsStatistics(@RequestBody UsersOperationsParams params){
        return R.ok(userOperationsService.queryUserOperationsStatistics(params));

    }

    /**
     * XX用户-XX业务-操作详情
     * @param params
     * @return 通用返回对象
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodes",  		value = "业务编码",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "dataCenterList", 		value = "数据中心",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "time", 			value = "时间",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "userIdentify", 			value = "用户标识）", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
    @PostMapping("/getUserBusinessOperationsDetail")
	@ApiOperation(value = "XX用户-XX业务-操作详情", notes = "XX用户-XX业务-操作详情")
    public R getUserBusinessOperationsDetail(@RequestBody UsersOperationsParams params){
        return R.ok(userOperationsService.queryUserBusinessOperationsDetail(params));
    }

    /**
     * XXX用户操作轨迹
     * @param params
     * @return 通用返回对象
     */
	@ApiImplicitParams({
			@ApiImplicitParam(name = "businessCodes",  		value = "业务编码",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "dataCenterList", 		value = "数据中心",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "time", 			value = "时间",  		paramType = "query", required = false, dataType = "List<Integer>"),
			@ApiImplicitParam(name = "endTime", 			value = "结束时间", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "startTime", 			value = "开始时间", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageNum",   			value = "页码", 				paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "pageSize", 			value = "每页大小", 			paramType = "query", 	required = true, 	dataType = "Long"),
			@ApiImplicitParam(name = "orderByColumn", 			value = "排序字段", 			paramType = "query", 	required = true, 	dataType = "String"),
			@ApiImplicitParam(name = "userIdentify", 			value = "用户标识）", 			paramType = "query", 	required = true, 	dataType = "String"),
	})
    @PostMapping("/getUserOperationsTrajectory")
	@ApiOperation(value = "XXX用户操作轨迹", notes = "XXX用户操作轨迹")
    public R getUserOperationsTrajectory(@RequestBody UsersOperationsParams params){
        return  R.ok(userOperationsService.queryUserOperationsTrajectory(params));

    }

    /**
     * 导出用户操作概况列表Excel文件
     *
     * @param params 用户操作概况查询参数
     * @return
     */
    @PostMapping("/downloadUsersOperationsOverview")
	@ApiOperation(value = "导出用户操作概况列表Excel文件", notes = "导出用户操作概况列表Excel文件")
    public void downloadUsersOperationsOverview(@RequestBody UsersOperationsParams params, HttpServletResponse response){
        try {
            userOperationsService.exportUsersOperationsOverview(params, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 导出XX用户操作统计列表Excel文件
     *
     * @param params 用户操作概况查询参数
     * @return
     */
    @PostMapping("/downloadUserOperationsStatistics")
	@ApiOperation(value = "导出XX用户操作统计列表Excel文件", notes = "导出XX用户操作统计列表Excel文件")
    public void downloadUserOperationsStatistics(@RequestBody UsersOperationsParams params, HttpServletResponse response){
        //获取表名.由于该查询不会跨天,同时详单表会记录所有开始时间在当天的详单,所以只需要根据开始时间查询一张详单表就可以.
        String tableName = "busi_detail_user_stat_m_";
        //时间格式为yyyy-MM-dd hh:mm:ss,先去除时分秒
        String[] time1 = params.getStartTime().split(" ");
        //再去除"-",使时间变为yyyyMMdd
        String[] time2 = time1[0].split("-");
        //拼凑表名,并放入params
        for (String s : time2) {
            tableName += s;
        }
        List<String> tablenNameList = new ArrayList<>();
        tablenNameList.add(tableName);
        params.setTableNameList(tablenNameList);
        try {
            userOperationsService.exportUserOperationsStatistics(params, response);
        } catch (Exception e) {
            e.printStackTrace();
//            ResponseUtil.setResponseResult(R
//                    .setFail(ApiResultCode.EXPORT_EXCEL_ERROR.getCode(), ApiResultCode.EXPORT_EXCEL_ERROR.getMsg()), response);
        }
    }

    /**
     * 导出XX用户XX业务操作详情列表Excel文件
     *
     * @param params 用户操作统计查询参数
     * @return 通用返回对象
     */
    @PostMapping("/downloadUserBusinessOperationsDetail")
	@ApiOperation(value = "导出XX用户XX业务操作详情列表Excel文件", notes = "导出XX用户XX业务操作详情列表Excel文件")
    public void downloadUserBusinessOperationsDetail(@RequestBody UsersOperationsParams params, HttpServletResponse response){
        //获取表名.由于该查询不会跨天,同时详单表会记录所有开始时间在当天的详单,所以只需要根据开始时间查询一张详单表就可以.
        String tableName = "business_detail_";
        //时间格式为yyyy-MM-dd hh:mm:ss,先去除时分秒
        String[] time1 = params.getStartTime().split(" ");
        //再去除"-",使时间变为yyyyMMdd
        String[] time2 = time1[0].split("-");
        //拼凑表名,并放入params
        for (String s : time2) {
            tableName += s;
        }
        List<String> tablenNameList = new ArrayList<>();
        tablenNameList.add(tableName);
        params.setTableNameList(tablenNameList);
        try {
            userOperationsService.exportUserBusinessOperationsDetail(params, response);
        } catch (Exception e) {
            e.printStackTrace();
//            ResponseUtil.setResponseResult(R
//                    .setFail(ApiResultCode.EXPORT_EXCEL_ERROR.getCode(), ApiResultCode.EXPORT_EXCEL_ERROR.getMsg()), response);
        }
    }
}
